<?php
	
function confirm_query($results) {
	if(!$results) {
		die("Blad zapytania: " . mysql_error());
	}
}
	
function main_navigation() {
	echo "<h2>Menu:</h2>";
	echo "<ul>";
		echo "<li><a href=\"panel.php\">strona główna panelu</a></li>";
		echo "<li><a href=\"panel.php?action=reklamacje\">reklamacje</a></li>";
	echo "</ul>";
}
	
function main_panel_page() {
	echo "<h2>Jakaś inna usługa</h2>";
	echo "<p>Coś tam innego</p>";
}
	
function main_complain_panel() {
	get_all_open_complaints();
	get_all_closed_complaints();
}

function main_user_complain_panel($user_id) {
	get_open_complaints($user_id);
	get_closed_complaints($user_id);
}
	
function create_new_complaint() {
	echo "<h2><a href=\"panel.php?action=reklamuj\">> stwórz nową reklamację</a></h2>";
}

	
function new_complaint($user_id) {
	echo "<h2>Nowa reklamacja</h2>";
?>
	<form method="post" action="new_complaint.php">
		<input type="hidden" name="user_id" value="<?php echo $user_id; ?>">
		<p>Temat: 
			<p><input type="text" name="title" value="" size="50" maxlenght="50"></p>
		</p>
		<p>Tresc:
			<p><textarea name="content" cols="70" rows="20"></textarea></p>
		</p>
		<input type="submit" value="Dodaj reklamację">
	</form>

<?php
}
	
function new_reply($complaint_id, $user_id) {
	global $connection;
	
	if (isset($_POST['what'])) {
		$what = $_POST['what'];
		$id = $_GET['id'];
		close_open_complaint($what, $id);
	}

	$query = "SELECT id, is_closed ";
	$query .= "FROM complaint ";
	$query .= "WHERE id = $complaint_id ";
	$query .= "LIMIT 1";
			
	$select_complaints = mysql_query($query, $connection);
	confirm_query($select_complaints);
		
while($complaint = mysql_fetch_array($select_complaints)) {
	if($complaint["is_closed"] == 1) {
		echo "<h2>Odpowiedzi zamknięte!</h2>";
		
	} else {
	
?>
		<p><form method="post" action="new_reply.php">
			<input type="hidden" name="com_id" value="<?php echo $complaint_id; ?>">
			<input type="hidden" name="user_id" value="<?php echo $user_id; ?>">
			<p><h3>Dodaj odpowiedź:</h3>
				<p><textarea name="content" cols="50" rows="5"></textarea></p>
			</p>
			<input type="submit" value="Dodaj odpowiedz">
		</form></p>
<?php

	}
	
	if($_COOKIE['superuser'] == 1) {
	
	echo "<div class=\"admin\"><p>";
	echo "<form method=\"post\" action=\"panel.php?action=reklamacja&id=" . $complaint["id"] . "\">";
		echo "<select name=\"what\">";
			if ($complaint["is_closed"] == 1) {
		    	echo "<option value=\"0\">Otworz reklamacje</option>";
			} else {
			    echo "<option value=\"1\">Zamknij reklamacje</option>";
		    }
		echo "</select>";
		echo " <input type=\"submit\" value=\"zmien\">";
	echo "</form>";
	echo "</div></p>";
	
	}
}	


}

function close_open_complaint($what, $where) {
	global $connection;
	
	$query = "UPDATE complaint ";
	$query .= "SET is_closed= $what ";
	$query .= "WHERE id= $where";

	$open_close = mysql_query($query, $connection);
		confirm_query($open_close);
}

function get_all_open_complaints() {
	global $connection;

	$query = "SELECT id, title, date ";
	$query .= "FROM complaint ";
	$query .= "WHERE is_closed = 0 ";
	$query .= "ORDER BY `date` DESC";

	$open_complains = mysql_query($query, $connection);
	confirm_query($open_complains);
	
	echo "<h2>Wszystkie otwarte reklamacje:</h2>";
		echo "<p>";
		echo "<ul>";
			while($complaint = mysql_fetch_array($open_complains)) {
				echo "<li><a href=\"panel.php?action=reklamacja&id=" . $complaint["id"] . "\">" . $complaint["title"] . "</a><br>" . "Stworzono dnia: " . $complaint["date"] . "</li>";
			}	
		echo "</ul>";
		echo "</p>";
}

function get_all_closed_complaints() {
	global $connection;
	
	$query = "SELECT id, title, date ";
	$query .= "FROM complaint ";
	$query .= "WHERE is_closed = 1 ";	
	$query .= "ORDER BY `date` DESC";

	$closed_complains = mysql_query($query, $connection);
	confirm_query($closed_complains);
		
	echo "<h2>Wszystkie archiwalne reklamacje:</h2>";
		echo "<p>";
		echo "<ul>";
			while($complaint = mysql_fetch_array($closed_complains)) {
				echo "<li><a href=\"panel.php?action=reklamacja&id=" . $complaint["id"] . "\">" . $complaint["title"] . "</a><br>" . "Stworzono dnia: " . $complaint["date"] . "</li>";
			}	
		echo "</ul>";
		echo "</p>";
}

function get_open_complaints($user_id) {
	global $connection;

	$query = "SELECT id, title, date, owner_id ";
	$query .= "FROM complaint ";
	$query .= "WHERE is_closed = 0 ";
	$query .= "AND owner_id = $user_id ";
	$query .= "ORDER BY `date` DESC";
	
	$open_complains = mysql_query($query, $connection);
	confirm_query($open_complains);
	
	echo "<h2>Wszystkie twoje otwarte reklamacje:</h2>";
		echo "<p>";
		echo "<ul>";
			while($complaint = mysql_fetch_array($open_complains)) {
				echo "<li><a href=\"panel.php?action=reklamacja&id=" . $complaint["id"] . "\">" . $complaint["title"] . "</a><br>" . "Stworzono dnia: " . $complaint["date"] . "</li>";
			}	
		echo "</ul>";
		echo "</p>";
}

function get_closed_complaints($user_id) {
	global $connection;
	
	$query = "SELECT id, title, date, owner_id ";
	$query .= "FROM complaint ";
	$query .= "WHERE is_closed = 1 ";
	$query .= "AND owner_id = $user_id ";
	$query .= "ORDER BY `date` DESC";

	$closed_complains = mysql_query($query, $connection);
	confirm_query($closed_complains);
		
	echo "<h2>Twoje wszystkie archiwalne reklamacje:</h2>";
		echo "<p>";
		echo "<ul>";
			while($complaint = mysql_fetch_array($closed_complains)) {
				echo "<li><a href=\"panel.php?action=reklamacja&id=" . $complaint["id"] . "\">" . $complaint["title"] . "</a><br>" . "Stworzono dnia: " . $complaint["date"] . "</li>";
			}	
		echo "</ul>";
		echo "</p>";
}

function get_complain($id) {
	global $connection;
	
	$query = "SELECT title, content, is_closed, date, username, owner_id ";
	$query .= "FROM complaint ";
	$query .= "INNER JOIN users ON ( complaint.owner_id = users.id ) ";
	$query .= "WHERE complaint.id = $id ";
	$query .= "LIMIT 1";
				
	$select_complaints = mysql_query($query, $connection);
	confirm_query($select_complaints);
			
	while($complaint = mysql_fetch_array($select_complaints)) {
		echo "<h2>" . $complaint["title"] . "</h2>";
		echo "<h3>Autor: " . $complaint["username"] . ", data: " . $complaint["date"] . "</h3>";
		echo "<p>" . $complaint["content"] . "</p>";
	}	

}
	
function get_posts($complaint_id) {
	global $connection;
	
	$query = "SELECT content, date, username, owner_id, superuser ";
	$query .= "FROM posts ";
	$query .= "INNER JOIN users ON ( posts.owner_id = users.id ) ";
	$query .= "WHERE posts.complain_id = $complaint_id ";
	$query .= "ORDER BY `date` DESC ";
	$query .= "LIMIT 0 , 5";
	
	$select_posts = mysql_query($query, $connection);
	confirm_query($select_posts);
	
	echo "<hr>";
	new_reply($complaint_id, $_COOKIE['user_id']);
	
	if (get_rows_number($complaint_id)>0) {
		echo "<h2>Ostatnie odpowiedzi (od najnowszej):</h2>";
	} else {
		echo "<h2>Brak odpowiedzi</h2>";
	}
			
	while($post = mysql_fetch_array($select_posts)) {
		if($post["superuser"] == 1) {
			echo "<div class=\"admin\"><p>";
		} else {
			echo "<div><p>";
		}
			
		echo "<h3>Autor: " . $post["username"] . ", data: " . $post["date"] . "</h3>";
		echo $post["content"] . "</p></div>";
	}	
	
	if (get_rows_number($complaint_id)>5) {
		echo "<p><button type=\"button\" onclick=\"showPosts($complaint_id)\">Wczytaj starsze wiadomosci</button></p>";
		echo "<div id=\"starsze\"></div>";
	}
}

function get_rows_number($complaint_id) {
	global $connection;
	
	$query = "SELECT COUNT(*) AS licz ";
	$query .= "FROM posts ";
	$query .= "INNER JOIN users ON ( posts.owner_id = users.id ) ";
	$query .= "WHERE posts.complain_id = $complaint_id ";
	
	$rows = mysql_query($query, $connection);
	confirm_query($rows);
			
	while($row = mysql_fetch_array($rows)) {
		$wiersze = $row["licz"];
	}	
	
	return $wiersze;
}

?>